package com.nit.lab.service.basic.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.nit.lab.dto.basic.UserDTO;
import com.nit.lab.dto.utils.PagingDTO;
import com.nit.lab.dto.utils.TableReqDTO;
import com.nit.lab.dto.utils.TableRspDTO;
import com.nit.lab.entity.basic.UserEntity;
import com.nit.lab.mapper.basic.UserMapper;
import com.nit.lab.service.basic.UserService;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.Date;

@Service
public class UserServiceImpl implements UserService {

    @Autowired(required = false)
    private UserMapper userMapper;

    @Override
    public TableRspDTO list4Table(TableReqDTO tableReqDTO) throws Exception {
//        // 以下这段是为了演示日期区间内查询功能
//        String startDate = null;
//        String endDate = null;
//        if (tableReqDTO.parseQueryParam("gmtCreate") != null) {
//            JSONArray jsonObject = JSONObject.parseArray(tableReqDTO.parseQueryParam("gmtCreate"));
//            startDate = jsonObject.get(0).toString();
//            endDate = jsonObject.get(1).toString();
//        }
        Long count = userMapper.count4Table(
                tableReqDTO.parseQueryParam("loginName"),
                tableReqDTO.parseQueryParam("userName"),
                tableReqDTO.parseQueryParam("roleId"),
                tableReqDTO.parseQueryParam("userPhone"));
        PagingDTO pagingDTO = new PagingDTO(
                tableReqDTO.getPageNo(),
                tableReqDTO.getPageSize(),
                count,
                userMapper.list4Table(
                        tableReqDTO.getStart(),
                        tableReqDTO.getPageSize(),
                        tableReqDTO.parseQueryParam("loginName"),
                        tableReqDTO.parseQueryParam("userName"),
                        tableReqDTO.parseQueryParam("roleId"),
                        tableReqDTO.parseQueryParam("userPhone"),
                        tableReqDTO.getSortField(),
                        tableReqDTO.getSortOrder()));
        TableRspDTO tableRspDTO = new TableRspDTO(pagingDTO);
        return tableRspDTO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer add(UserDTO userDTO) throws Exception {
        MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        UserEntity userEntity = mapperFactory.getMapperFacade().map(userDTO, UserEntity.class);
        userEntity.setUserImage("https://gw.alipayobjects.com/zos/rmsportal/jZUIxmJycoymBprLOUbT.png");
        userEntity.setLoginPwd(DigestUtils.md5DigestAsHex("lab205@nit".getBytes()));
        userEntity.setGmtCreate(new Date());
        userEntity.setGmtModified(new Date());
        return userMapper.add(userEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer update(UserDTO userDTO) throws Exception {
        MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        UserEntity userEntity = mapperFactory.getMapperFacade().map(userDTO, UserEntity.class);
        userEntity.setGmtModified(new Date());
        return userMapper.update(userEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer remove(String userIds) throws Exception {
        String[] arrayUserId = userIds.split(",");
        for (int i = 0; i < arrayUserId.length; i++) {
            userMapper.remove(Long.parseLong(arrayUserId[i]));
        }
        return arrayUserId.length;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer resetPwd(Long userId, String loginPwd) throws Exception {
        return userMapper.resetPwd(userId, DigestUtils.md5DigestAsHex(loginPwd.getBytes()));
    }

    @Override
    public UserDTO loginCheck(String loginName) throws Exception {
        return userMapper.getByLoginName(loginName);
    }

    @Override
    public UserDTO getByUserId(Long userId) throws Exception {
        return userMapper.getByUserId(userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long updateImage(Long userId, String userImage) throws Exception {
        userMapper.updateImage(userId, userImage);
        return userId;
    }
}
